package com.letv.whatslive.model.convert;

import com.letv.whatslive.common.utils.ObjectUtils;
import com.letv.whatslive.model.GeneralCounter;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * Created by haojiayao on 16-6-23.
 */
public class GeneralCounterConvert {

    public static DBObject castGeneralCounterToDBObject(GeneralCounter generalCounter) {

        DBObject dbo = new BasicDBObject();

        if (generalCounter.getId() != null) {

            dbo.put("_id", generalCounter.getId());
        }

        dbo.put("type", generalCounter.getType());
        dbo.put("field", generalCounter.getField());
        dbo.put("count", generalCounter.getCount());

        return dbo;
    }

    public static GeneralCounter castDBObjectToGeneralCounter(DBObject dbObject) {

        GeneralCounter generalCounter = new GeneralCounter();

        if (dbObject != null) {

            generalCounter.setId(ObjUtils.toLong(dbObject.get("_id")));
            generalCounter.setType(ObjectUtils.toInteger(dbObject.get("type")));
            generalCounter.setField(ObjectUtils.toString(dbObject.get("field")));
            generalCounter.setCount(ObjectUtils.toLong(dbObject.get("count")));
        }

        return generalCounter;
    }
}
